package com.shut.vehicle.service.impl;

import com.github.pagehelper.PageHelper;
import com.shut.vehicle.configuration.AlibabaOcr;
import com.shut.vehicle.dao.CardNumDao;
import com.shut.vehicle.exception.ServiceException;
import com.shut.vehicle.pojo.CarNum;
import com.shut.vehicle.service.CardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;

/**
 * @author 卢斌
 */
@Service
public class CardInfoServiceImpl implements CardInfoService {


    @Autowired
    private CardNumDao cardNumDao;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCardInfo(MultipartFile file, Integer userId) {
        CarNum carNum = AlibabaOcr.releaseCarNum(file);
        if (carNum == null) {
            throw new ServiceException("无效的车辆行驶证照片");
        }
        carNum.setUserId(userId);
        boolean exist = cardNumDao.checkCardExist(carNum.getCarNum());
        if (exist) {
            throw new ServiceException("该车牌号已被认证过");
        }
        cardNumDao.addCardNum(carNum);
    }


    @Override
    public List<CarNum> getCarNum(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return cardNumDao.listCardNum(userId);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCardInfo(Integer cardNumId, Integer userId) {
        cardNumDao.deleteCardNum(cardNumId, userId);
    }


}
